/*
Copyright 2010 Farent-Solutions Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 */
package com.farent.solutions.plugins.aribaweb;

import java.io.IOException;
import org.apache.tools.ant.module.api.support.ActionUtils;
import org.netbeans.modules.java.api.common.ant.UpdateHelper;
import org.netbeans.spi.project.ActionProvider;
import org.netbeans.spi.project.support.ant.PropertyEvaluator;
import org.netbeans.spi.project.ui.support.DefaultProjectOperations;
import org.openide.filesystems.FileObject;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;

/**
 *
 * @author user
 */
public class NBawActionProvider implements ActionProvider {

    // Project
    final AWNetbeansProject project;
    // Ant project helper of the project
    private UpdateHelper updateHelper;
    //Property evaluator
    private final PropertyEvaluator evaluator;

    public NBawActionProvider( AWNetbeansProject project, UpdateHelper updateHelper ) {
        this.project = project;
        this.updateHelper = updateHelper;
        this.evaluator = project.getEval();
    }
    private String[] supported = new String[]{
        ActionProvider.COMMAND_DELETE,
        ActionProvider.COMMAND_COPY,
        ActionProvider.COMMAND_BUILD,
        ActionProvider.COMMAND_CLEAN,
        ActionProvider.COMMAND_REBUILD,
        ActionProvider.COMMAND_RENAME,
        ActionProvider.COMMAND_MOVE,
        ActionProvider.COMMAND_RUN,};

    @Override
    public String[] getSupportedActions() {
        return supported;
    }

    @Override
    public void invokeAction(String command, Lookup lookup) throws IllegalArgumentException {
        if (COMMAND_DELETE.equals(command)) {
            DefaultProjectOperations.performDefaultDeleteOperation(project);
            return ;
        }

        if (COMMAND_COPY.equals(command)) {
            DefaultProjectOperations.performDefaultCopyOperation(project);
            return ;
        }

        if (COMMAND_MOVE.equals(command)) {
            DefaultProjectOperations.performDefaultMoveOperation(project);
            return ;
        }

        if (COMMAND_RENAME.equals(command)) {
            DefaultProjectOperations.performDefaultRenameOperation(project, null);
            return ;
        }
        //Here we find the Ant script and call the target we need!
        if (command.equals(ActionProvider.COMMAND_RUN)) {
            try {
                FileObject buildImpl = project.antProjectHelper.getProjectDirectory().getFileObject("build.xml");
                ActionUtils.runTarget(buildImpl, new String[]{"launch"}, null);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        if (command.equals(ActionProvider.COMMAND_CLEAN)) {
            try {
                FileObject buildImpl = project.antProjectHelper.getProjectDirectory().getFileObject("build.xml");
                ActionUtils.runTarget(buildImpl, new String[]{"clean"}, null);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        if (command.equals(ActionProvider.COMMAND_BUILD)) {
            try {
                FileObject buildImpl = project.antProjectHelper.getProjectDirectory().getFileObject("build.xml");
                ActionUtils.runTarget(buildImpl, new String[]{"war"}, null);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        if (command.equals(ActionProvider.COMMAND_REBUILD)) {
            try {
                FileObject buildImpl = project.antProjectHelper.getProjectDirectory().getFileObject("build.xml");
                ActionUtils.runTarget(buildImpl, new String[]{"clean","war"}, null);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    @Override
    public boolean isActionEnabled(String command, Lookup lookup) throws IllegalArgumentException {
        for (int i = 0; i < supported.length; i++) {
            String action = supported[i];
            if(action.equalsIgnoreCase(command))
                return true;
        }
        return true;
    }
}
